/*
 * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
 */
package com.wlh.animation.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * @author weilh
 *         <b>DATE</b> 2016/7/26.
 */
public class TouchTestLinearLayout extends LinearLayout{

    private String mName;
    private boolean mInterceptTouch = false;

    private Boolean mDispatchResult;

    private Boolean mTouchResult;

    public void setDispatchResult(Boolean dispatchResult) {
        this.mDispatchResult = dispatchResult;
    }

    public void setTouchResult(Boolean touchResult) {
        this.mTouchResult = touchResult;
    }

    public TouchTestLinearLayout(Context context) {
        super(context);
    }

    public TouchTestLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TouchTestLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setName(String name) {
        this.mName = name;
    }

    public void setInterceptTouch(boolean interceptTouch) {
        this.mInterceptTouch = interceptTouch;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.i("wlh", mName + " : dispatchTouchEvent : " + getActionName(ev.getAction()));
        boolean result = super.dispatchTouchEvent(ev);
        Log.i("wlh", mName + " : dispatchTouchEvent : " + result );
        if (mDispatchResult != null) {
            return mDispatchResult;
        }
        return result;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.i("wlh", mName + " : onInterceptTouchEvent : " + getActionName(ev.getAction()));
        if (mInterceptTouch) return true;
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        Log.i("wlh", mName + " : onTouchEvent : " + getActionName(ev.getAction()));
        boolean result = super.onTouchEvent(ev);
        Log.i("wlh", mName + " : onTouchEvent : " + result );
        if (mTouchResult != null) {
            return mTouchResult;
        }
        return result;
    }

    public String getActionName(int action) {
        switch (action) {
            case MotionEvent.ACTION_DOWN :
                return "DOWN";
            case MotionEvent.ACTION_UP :
                return "UP";
            case MotionEvent.ACTION_CANCEL :
                return "CANCEL";
            case MotionEvent.ACTION_MOVE :
                return "MOVE";
        }
        return "OTHER";
    }

}
